package com.the4man.framework.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.cache.PermissionCache;
import com.the4man.framework.pagemodel.PUser;
import com.the4man.framework.utils.RequestUtil;
import org.apache.commons.lang.StringUtils;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;

/**
 * Created by lenovo on 2017/12/12.
 */
public class AuthorityInterceptor extends AbstractInterceptor {
    private static final long serialVersionUID = 1L;
    @Resource
    private PermissionCache permissionCache;

    public AuthorityInterceptor() {
    }

    public void init() {
        super.init();
    }

    public String intercept(ActionInvocation invocation) throws Exception {
        ActionContext actionContext = invocation.getInvocationContext();
        HttpServletRequest httpRequest = (HttpServletRequest)actionContext.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
        PUser user = RequestUtil.getSessionUserinfo();
        BaseAction baseAction = (BaseAction)invocation.getAction();
        String uri = this.parseUri(httpRequest);
        System.out.println("访问地址：" + uri);
        return user == null?invocation.invoke():(this.checkPermission(httpRequest, uri, user.getRoleid())?"unPermission":invocation.invoke());
    }

    private boolean checkPermission(HttpServletRequest httpRequest, String uri, String roleId) {
        HashSet<String> needToVerifyPermissions = this.permissionCache.getNeedToVerifyPermissions();
        Iterator var6 = needToVerifyPermissions.iterator();

        String suri;
        while(var6.hasNext()) {
            suri = (String)var6.next();
            System.out.println(suri);
        }

        suri = StringUtils.startsWith(uri, "/")?StringUtils.substringAfter(uri, "/"):uri;
        String cnbdwStart = StringUtils.startsWith(suri, "cnbdw")?suri:"cnbdw/" + suri;
        String cnbdwStart1 = "/" + cnbdwStart;
        String start = StringUtils.startsWith(suri, "cnbdw")?StringUtils.substringAfter(cnbdwStart, "cnbdw/"):suri;
        String start1 = "/" + start;
        if(needToVerifyPermissions != null && !needToVerifyPermissions.isEmpty() && (needToVerifyPermissions.contains(cnbdwStart) || needToVerifyPermissions.contains(cnbdwStart1) || needToVerifyPermissions.contains(start) || needToVerifyPermissions.contains(start1))) {
            HashSet<String> permissions = this.permissionCache.getPermissionByRoleId(roleId);
            return permissions != null && !permissions.isEmpty()?!permissions.contains(cnbdwStart) && !permissions.contains(cnbdwStart1) && !permissions.contains(start) && !permissions.contains(start1):true;
        } else {
            return false;
        }
    }

    private String parseUri(HttpServletRequest request) {
        String contextPath = request.getContextPath();
        String uri = request.getRequestURI();
        if(!contextPath.equals("/")) {
            uri = uri.substring(contextPath.length());
        }

        return uri;
    }

    private String forwarePage(HttpServletRequest httpRequest, BaseAction baseAction, String uri) throws IOException, ServletException {
        return uri != null && uri.indexOf("ajaxJson") != -1?(uri != null && uri.indexOf("ajaxJson") != -1?"missSession":"missSession"):"missSession";
    }
}
